<script setup>
// TODO: 编辑
import {ref,defineExpose } from 'vue'
import axios from 'axios'
// 弹框开关
const dialogVisible = ref(false)
// 1.显示弹框的方法 2.接收父组件传递过来的数据
const form = ref({
  name:'',
  place:'',
  id:''
})
const showDialog = (row)=>{
  dialogVisible.value = true
  form.value.name = row.name
  form.value.place = row.place
  form.value.id = row.id
}
// 将显示弹窗的方法传递给父组件
defineExpose({
  showDialog
})
// 更新表单数据
const emit = defineEmits(['on-updata'])
const onUpdata = async() =>{
  axios.patch(`/edit/${form.value.id}`, {
    name: form.value.name,
    place: form.value.place,
    id:form.value.id
  })
  dialogVisible.value = false
  emit('on-updata')
}


</script>

<template>
  <el-dialog v-model="dialogVisible" title="编辑" width="400px">
    <el-form :data="form" label-width="50px">
      <el-form-item label="姓名">
        <el-input v-model="form.name" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="籍贯">
        <el-input v-model="form.place" placeholder="请输入籍贯" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="onUpdata">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped>
.el-input {
  width: 290px;
}
</style>
